package com.p7.framework.common.validator;

import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

@Component
public class LocaleService {

	@Autowired
	private MessageSource messageSource;

	public String getMessage(Locale locale, String code) {

		String message = messageSource.getMessage(code, new Object[] {}, locale);
		if (StringUtils.isNotBlank(message)) {
			return message;
		}
		return code;
	}

	public String getMessage(String code) {
		Locale locale = LocaleContextHolder.getLocale();
		String message = messageSource.getMessage(code, new Object[] {}, locale);
		if (StringUtils.isNotBlank(message)) {
			return message;
		}
		return code;
	}
}
